const insertServicse = require("../servicse/muchtab")
let i = 0;
const add = async (req, res) => {
    let fields = req.body;
    console.log(i)
    let renewal;
    let num = "G000000000";
    let sql = `supplier`;
    if (fields.suppliernumber == "") {
        let sel= await insertServicse.select1(sql);
        function a() {
            i++;
            let iStr = i.toString();
            let iLen = iStr.length;
            if (iLen <= num.match(/0+/)[0].length) {
                let diff = num.match(/0+/)[0].length - iLen;
                renewal = num.replace(/0+/g, match => match.slice(0, diff) + i);
            }
        }
        a()
        sel.forEach(itme=>{
            if(itme.suppliernumber==renewal){
                a()
            }
        })
        fields.suppliernumber=renewal;
    }

    let routs = await insertServicse.add(sql, fields)
    // console.log(routs)
    res.send({ code: 0, msg: 'success', data: routs.insertId });
}
const select = async (req, res) => {
    // console.log(req.body);
    let sql = `SELECT a.suppliernumber,a.suppliername,b.category,a.start FROM supplier a LEFT JOIN suppliercategory b on a.suppliercategory=b.id`;
    let routs = await insertServicse.selectmore(sql);
    res.send(routs);
}
module.exports = {
    add,
    select
}